// pages/fooddetail/fooddetail.js
var app = getApp();
var qqmapsdk;
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var wxParse = require('../../../wxParse/wxParse.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 自带弹框
    // text:"这是一个页面"
    hiddenModal: true,
    hide:true,
    id: '',//菜品id
    foodObj:{},//当前菜品的大数据
    userObj:{},
    location:"",
    chosedFoodCount: 0,
    hiddenModal: true,
    address:'',
    carArray:[],
    is_t:false,
  },
  ytuijian:function(){
      wx.showToast({
        title: '已经推荐过了',
      })
  },
  tuijian:function(e){
      var foodObj = this.data.foodObj;
      var user_id = wx.getStorageSync('users').id
      foodObj.tuijian = parseInt(foodObj.tuijian)+1;
      var that = this;
      app.util.request({
        'url': 'entry/wxapp/GoodsInfoTui',
        'cachetime': '0',
        data: { user_id: user_id,goods_id:foodObj.id},
        success: function (res) {
          console.log(res)
          that.setData({
            foodObj: foodObj,
            is_t:true,
          })
          wx.showToast({
            title: '推荐成功',
          })
        }
      })
     
  },
  // 弹框
  listenerButton: function () {
    var that = this;
    var user_id = wx.getStorageSync('users').id
    that.useAddress(user_id);
    this.setData({
      hiddenModal: !this.data.hiddenModal,
      hide: !this.data.hide,
    })
  },

  listenerConfirm: function () {
    this.setData({
      hiddenModal: true,
      hide: false,
    })
  },
  useAddress: function (user_id) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/UseAddress',
      'cachetime': '0',
      data: { user_id: user_id },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            address: res.data.data
          })
        } else {
          that.setData({
            address: false
          })
        }
      }
    })
  },
  //根据id获取菜品详情
  getDetailById: function (id) {
    let that = this;
    app.util.request({
      'url': 'entry/wxapp/AppDishesInfo',//搜索菜品接口
      'cachetime': '0',
      'data': { "id": id },
      success: function (res) {
        wxParse.wxParse('detail', 'html', res.data.details, that, 0);
        that.setData({
          "foodObj": res.data
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    let id= e.id,
        that = this;
    this.setData({"id":id});
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: "#ffffff",
    })
    wx.setNavigationBarTitle({ title: '商品详情' })
    that.getDetailById(id);
    var carArr = wx.getStorageSync('carArr');
    that.setData({
      carArray:carArr
    })
    wx.getStorage({
      key: 'imglink',
      success: function (res) {
        that.setData({ "imgLink": res.data });
      }
    })
    wx.getStorage({
      key: 'store',
      success: function (res) {
        that.setData({ "store": res.data });
      }
    })
    wx.getStorage({
      key: 'users',
      success: function (res) {
        that.setData({ "userObj": res.data });
        // app.getMyorder(13);
      }
    })
    wx.getStorage({
      key: 'chosedFood',
      success: function (res) {
        console.log("chosedFood")
        let len = 0;
        for (let key in res.data){
          len ++
          console.log(len)
        }
        console.log(len)
        that.setData({ "chosedFood": res.data, "chosedFoodCount": len });
      }
    })
    // wx.getStorage({
    //   key: 'userLocation',
    //   success: function (res) {
    //     that.setData({ "location": res.data });
    //     // that.findLocation();
    //   }
    // })
    // wx.getStorage({
    //   key: 'bqxx',
    //   success: function (res) {
    //     that.setData({ "mapKey": res.data.map_key });
    //     // that.findLocation();
    //   }
    // })
    
  },
  
  // findLocation: function (latitude, longitude){
  //   console.log("获取地理位置")
  //   qqmapsdk.reverseGeocoder({
  //     location: {
  //       latitude: latitude,
  //       longitude: longitude
  //     },
  //     coord_type: 3,
  //     success: function (res) {
  //       console.log(res);
  //       console.log('坐标转地址后的经纬度起点：', res.result.ad_info.location)
  //       that.setData({
  //         start: res.result.ad_info.location,
  //         qzf: false,
  //       })
  //     },
  //     fail: function (res) {
  //       console.log(res);
  //     },
  //     complete: function (res) {
  //       console.log(res);
  //     }
  //   });
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  plusNumEvent: function (e) {
    let curGoodId = e.currentTarget.id,
      chosedFood = this.data.chosedFood,
      num, obj;
    for (let key in chosedFood) {
      if (curGoodId == chosedFood[key].id) {
        obj = chosedFood[key];
        if (!!chosedFood[key].choseNum) {
          num = chosedFood[key].choseNum + 1;
        } else {
          num = 1;
        }
        obj["choseNum"] = num;
        chosedFood[key] = obj;
        this.setData({ "chosedFood": chosedFood});
        app.changeChosedFood(curGoodId, obj);
        break;
      }
    }
  },
  reduceNumEvent: function (e) {
    let curGoodId = e.currentTarget.id,
      chosedFood = this.data.chosedFood,
      num, obj;
    for (let key in chosedFood) {
      if (curGoodId == chosedFood[key].id) {
        obj = chosedFood[key];
        if (!!chosedFood[key].choseNum) {
          num = chosedFood[key].choseNum - 1;
        } else {
          num = 0;
        }
        obj["choseNum"] = num <= 0 ? 0 : num;
        if (obj["choseNum"] <= 0){
          delete chosedFood[key];
          app.changeChosedFood(curGoodId, obj ,false);
          //改变chosedFoodCount
          this.setData({ "chosedFoodCount": this.data.chosedFoodCount-1});
        }else{
          chosedFood[key] = obj;
          app.changeChosedFood(curGoodId, obj);
        }
        this.setData({ "chosedFood": chosedFood });
        break;
      }
    }
  },
  // 点击用户地址 跳转到 地址页面
  tochooseaddress:function(){
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },

  // 加入订单
  addOrder:function(){
    let foodObj = this.datafoodObj;
    app.changeChosedFood(foodObj.id,1);
  },
  // 去选择优惠券
  coupon: function (e) {
    var that = this
    console.log(that.data)
    wx: wx.navigateTo({
      url: '../coupons/mine_coupons?totalPrice=' + that.data.totalPrice + '&state=' + 1,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  addheorderclose: function () {
    this.setData({
      hiddenModal: true
    })
  },
  urls: function () {
    wx.navigateTo({
      url: '/zh_dianc/pages/addresslist/addresslist',
    })
  },
  yurls: function () {
    wx.navigateTo({
      url: '/zh_dianc/pages/coupons/mine_coupons?totalPrice=' + this.data.totalPrice + '&state=1',
    })
  },
  listenerCancel: function () {
    this.setData({
      hiddenModal: true
    })
  },
})